﻿/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Diagnostics;
using System.Data.SQLite;

namespace IvyOrm.Test
{
    [TestClass]
    [DeploymentItem("x86")] //SQLite
    public class IvyOrmOptionsTest
    {
        [TestMethod]
        public void OutputToTraceSource()
        {
            var sb = new StringBuilder();

            //With Tracing On
            using (var stringWriter = new StringWriter(sb))
            using (var listener = new TextWriterTraceListener(stringWriter))
            {
                IvyOrm.Tracing.TraceSource.Listeners.Add(listener);

                try
                {
                    using (var connection = new SQLiteConnection("Data Source=:memory:;Version=3;New=True;"))
                    {
                        connection.Open();

                        var results = connection.ValueQuery<string>("select @0, 'test string'", "alpha").ToArray();
                        Assert.AreEqual("alpha", results[0]);
                    }

                    Assert.IsTrue(sb.ToString().Contains(@"'test string'"));
                    Assert.IsFalse(sb.ToString().Contains("@0"));
                }
                finally
                {
                    IvyOrm.Tracing.TraceSource.Listeners.Remove(listener);
                }
            }

            //With Tracing Off
            sb = new StringBuilder();
            using (var stringWriter = new StringWriter(sb))
            using (var listener = new TextWriterTraceListener(stringWriter))
            {
                IvyOrm.Tracing.TraceSource.Listeners.Add(listener);
                Assert.IsTrue(string.IsNullOrEmpty(sb.ToString()));

                try
                {
                    using (var connection = new SQLiteConnection("Data Source=:memory:;Version=3;New=True;"))
                    {
                        connection.Open();

                        var results = connection.ValueQuery<string>(new IvyOrmOptions { OutputToTraceSource = false },  "select @0, 'test string'", "alpha").ToArray();
                        Assert.AreEqual("alpha", results[0]);
                    }

                    Assert.IsFalse(sb.ToString().Contains(@"'test string'"));
                    Assert.IsTrue(string.IsNullOrEmpty(sb.ToString()));
                }
                finally
                {
                    IvyOrm.Tracing.TraceSource.Listeners.Remove(listener);
                }
            }
        }

    }
}
